export const engineTsCodeMap = {
    actInfo: `/**
 * 获取活动信息
 */
export const getEngineActInfoApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine!.getActInfo()
	return res
}

export type IEngineActInfoApiBody = Returned<typeof getEngineActInfoApi>['data']`,

    collection: `/**
 * 获取用户图鉴列表
 */
export const getEngineCollectionApi = async () => {
	const { engine, appConfig } = await useAppContext()
	const res = await engine!.getCollection({
		collectionKey: appConfig.CollectionKey as string,
	})
	return res
}

export type IEngineCollectionApiBody = Returned<typeof getEngineCollectionApi>['data']`,

    configs: `/**
 * 获取引擎配置
 */
export const getEngineConfigsApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine!.getActConfig()
	if (res.code !== 0) {
		return
	}
	const configs = res.data?.configs?.reduce(
		(acc, curr) => {
			const key = curr.key
			let value = curr.value
			try {
				value = JSON.parse(value)
			} catch {
				console.log('cannot parse', key, value)
			}
			acc[key] = value
			return acc
		},
		{} as Record<string, any>
	)
	return configs
}

export type IEngineConfigsApiBody = Returned<typeof getEngineConfigsApi>`,

    drawPreview: `/**
 * 获取引擎抽奖预览
 */
export const getEngineDrawPreviewApi = async (drawKey: string) => {
	const { engine } = await useAppContext()
	const res = await engine!.getDrawPreview({
		drawKey,
	})
	return res
}

export type IEngineDrawPreviewApiBody = Returned<typeof getEngineDrawPreviewApi>['data']`,

    exchanges: `/**
 * 获取引擎兑换列表
 */
export const getEngineExchangesApi = async () => {
	const { engine, appConfig } = await useAppContext()
	const { ExchangeKey } = appConfig
	const res = await engine!.getExchangeList({
		exchangeKey: ExchangeKey as string,
	})
	return res
}

export type IEngineExchangesApiBody = Returned<typeof getEngineExchangesApi>['data']`,

    levels: `/**
 * 获取等级列表
 */
export const getEngineLevelsApi = async () => {
	const { engine, appConfig } = await useAppContext()
	const LevelKey = appConfig
	const res = await engine!.getLevelList({
		levelKey: (LevelKey.LevelKey as string) || '',
	})
	return res
}

export type IEngineLevelsApiBody = Returned<typeof getEngineLevelsApi>['data']`,

    records: `/**
 * 获取抽奖记录
 */
export const getEngineRecordsApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine!.getRecordList({
		page: {
			page: 1,
			limit: 100,
		},
	})
	return res
}

export type IEngineRecordsApiBody = Returned<typeof getEngineRecordsApi>['data']`,

    rank: `/**
 * 获取总榜
 */
export const getEngineRankApi = async (rankKey: string) => {
	const { engine } = await useAppContext()
	const res = await engine!.getRankStruct<any>({
		rankId: rankKey,
	})
	return res
}

export type IEngineRankApiBody = Returned<typeof getEngineRankApi>['data']`,

    tasks: `/**
 * 获取引擎任务列表
 */
export const getEngineTasksApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine!.getTaskList({})
	return res
}

export type IEngineTasksApiBody = Returned<typeof getEngineTasksApi>['data']['tasks']`,

    rewardDetail: `/**
 * 获取引擎奖励详情
 */
export const getEngineRewardDetailApi = async (id: string, type: string) => {
	const { engine, appConfig } = await useAppContext()
	const res = await engine!.getPrizeDetail({
		id: id as string,
		type: type as string,
		actId: appConfig?.ActivityKey || '',
	})
	return res
}

export type IEngineRewardDetailApiBody = Returned<typeof getEngineRewardDetailApi>['data']`,
}

export type IEngineTsCodeMapKeys = keyof typeof engineTsCodeMap

export const engineTs = (keys: IEngineTsCodeMapKeys[]) => {
    const values = keys.map((key) => engineTsCodeMap[key])

    return `import { Returned, useAppContext } from './useAppContext'

${values.join('\n')}
`
}
